// ==UserScript==
// @name           kuxun ticket search
// @namespace      kuxun
// @include        http://piao.kuxun.cn/search.php?T=Ticket&Cat=sale*
// ==/UserScript==

var trains = ['K372', 'T34'];
var dates = ['1-17', '1-18', '1-19'];

function checkKeyWords(content)
{
	var isInTrains = 0;
	var isInDates = 0;
	for(var i = 0; i < trains.length; i++)
	{
		if(content.indexOf(trains[i]) >= 0)
		{
			isInTrains = 1;
			break;
		}
	}
	
	for(var i = 0; i < dates.length; i++)
	{
		if(content.indexOf(dates[i]) >= 0)
		{
			isInDates = 1;
			break;
		}
	}
	
	if(isInTrains > 0 && isInDates > 0)
		return true;
	else
		return false;
}


function parse_data()
{
	var div;
	var divs;

	divs = document.evaluate(
    "//DIV[@class='back']",
    document,
    null,
    XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
    null);

	if(divs.snapshotLength)
	{
		var found = 0;
			
		for(var i = 0; i < divs.snapshotLength; i++)
		{
			var div = divs.snapshotItem(i);
			
			// let node reach tag <A>
			node = div;
			for(var j = 0; node && j < 3; j++)
			{
				node = node.childNodes[1];
			}
			
			if(node)
			{
				var text = node.text;
				var href = node.href;
				if(checkKeyWords(text))
				{
					node.style.color = 'red';
					found += 1;
					GM_log("match..."+text+"\nlink = "+href);
				}
			}
		}
		
		if(found > 0)
		{
			alert("found " + found + " items in kuxun search\nthey are all highlighted...");
			}
	}
}


parse_data();

setTimeout("window.location.href=window.location.href", 60000);// auto reload every 60 sec